package com.global.college.utils;


import android.os.Build;
import androidx.annotation.RequiresApi;


import com.global.college.R;
import com.global.college.base.BaseActivity;
import com.global.college.ui.interfaces.ICacheView;
import com.global.college.views.CommDialog;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class HsSetCacheUtil {

    private String TAG="HsSetCacheUtil";
    private BaseActivity context;
    private String cacheData;
    String newCacheData = "";
    private HsCacheDealUtils  mHsCacheDealUtils;

    public HsSetCacheUtil(BaseActivity context) {
        this.context=context;
        mHsCacheDealUtils=new HsCacheDealUtils(context);
    }
    private ICacheView mICacheView;


    public void setICacheView(ICacheView iCacheView){
        mICacheView = iCacheView;
    }
    /**
     * 获取App的缓存大小
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public  void getAppCache() {
        Observable.just(cacheSize())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String result) throws Exception {
                        if(null != mICacheView){
                            mICacheView.onGetCacheSuccess(result);
                        }
                    }
                });
    }
    /**
     * 计算缓存
     */

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public String cacheSize() {
        if(context==null||mHsCacheDealUtils==null)  return  "0M";
        return mHsCacheDealUtils.getCacheSize();
    }

    /**
     * 清除缓存
     */
    public void cleanCache() {
        if(context==null)  return;
        CommDialog.newInstance(context).setClickListen(new CommDialog.TwoSelDialog() {
            @Override
            public void leftClick() {
            }
            @Override
            public void rightClick() {
                if(null != mICacheView){
                    mHsCacheDealUtils.clearCache();
                    mICacheView.onGetDeleteeCacheSuccess("0M");

                }
                ToastUtils.showCenterToast(context.getString(R.string.str_clear_cache_succ));
            }
        }).setTitle( context.getString(R.string.str_clear_cache_info)).show();
    }


}
